package cn.enilu.flash.mobile.controller;


import cn.enilu.flash.bean.dto.transport.TranspOrderSubmitDto;
import cn.enilu.flash.bean.dto.transport.UserAddressFindDto;
import cn.enilu.flash.bean.dto.transport.UserAddressSaveDto;
import cn.enilu.flash.bean.entity.shop.ShopUser;
import cn.enilu.flash.bean.entity.transport.TransportOrder;
import cn.enilu.flash.bean.entity.transport.UserAddress;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.exception.ApplicationExceptionEnum;
import cn.enilu.flash.bean.vo.front.Ret;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.transport.TransportOrderDetailVo;
import cn.enilu.flash.bean.vo.transport.UserAddressVo;
import cn.enilu.flash.service.shop.ShopUserService;
import cn.enilu.flash.service.transport.TransportOrderService;
import cn.enilu.flash.service.transport.UserAddressService;
import cn.enilu.flash.web.controller.BaseController;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
货运打车控制器
 */
@RestController
@RequestMapping("/transport")
public class TransportController extends BaseController {

    @Autowired
    private TransportOrderService transportOrderService;
    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private ShopUserService shopUserService;

    @PostMapping("order/submit")
    Ret submit(@RequestBody TranspOrderSubmitDto submitDto){
        TransportOrder orderInfo = transportOrderService.submitOrder(submitDto);
        return Rets.success(orderInfo);
    }

    @GetMapping("order/get")
    Ret getOrder(Long orderId){

        TransportOrderDetailVo orderDetail = transportOrderService.getOrderDetail(orderId);

        return Rets.success(orderDetail);

    }

    @PostMapping("address/save")
    Ret saveUserAddress(@RequestBody UserAddressSaveDto saveDto){

        ShopUser currentUser = shopUserService.getCurrentUser();

        if(currentUser == null){
            throw new ApplicationException(ApplicationExceptionEnum.USER_PERMISSION_ERROR);
        }
        saveDto.setUserId(currentUser.getId());
        UserAddressSaveDto pointInfo = userAddressService.save(saveDto);

        UserAddressVo vo = new UserAddressVo();
        UserAddressVo.MapPointVo pointVo = new UserAddressVo.MapPointVo();
        BeanUtil.copyProperties(pointInfo.getPoint(),pointVo);
        vo.setPoint(pointVo);
        vo.setAddressId(pointInfo.getId());
        vo.setContactName(pointInfo.getContactName());
        vo.setContactPhone(pointInfo.getContactPhone());
        vo.setAddressDetail(pointInfo.getAddressDetail());
        if(pointInfo.getPoint() != null){
            StringBuilder sb = new StringBuilder();
            if(StrUtil.isNotBlank(pointInfo.getPoint().getProvince())){
                sb.append(pointInfo.getPoint().getProvince());
            }
            if(StrUtil.isNotBlank(pointInfo.getPoint().getCity())){
                sb.append(pointInfo.getPoint().getCity());
            }
            if(StrUtil.isNotBlank(pointInfo.getPoint().getDist())){
                sb.append(pointInfo.getPoint().getDist());
            }
            vo.setCityDistrict(sb.toString());
        }
        return Rets.success(vo);
    }

    @GetMapping("address/queryByUser")
    public Ret getByUser(){
        Long idUser = getIdUser();

        UserAddressFindDto findDto = new UserAddressFindDto();
        findDto.setUserId(idUser);
        return Rets.success(userAddressService.queryAddressByUser(findDto));
    }
}
